<?php
defined('SYS_RUN') or exit();

class Curl {
   
   private $_o_curl; 
    
   private $_s_url = '';
   
   private $_a_headers = array();
   
   private $_s_post_data = '';
   
   private $_a_request_info = array();
   
   private $_s_user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
   
   private $_s_user = '';
   
   private $_b_show_header = false;
   
   private $_s_password = '';
   
   private $_i_port = 80;
   
   /*
    * Максимальное время на соединение с сервером
    */
   private $_i_connect_time_out = 80;
   
   /*
    * Время работы функции
    */
   private $_i_timeout = 10;
   
   public function set_url($s_url){
       $this->_s_url = $s_url;
       return $this;
   }
   
   public function show_header(){
       $this->_b_show_header = true;
       return $this;
   }
   
   public function set_header($s_head){
       $this->_a_headers[] = $s_head;
       return $this;
   }
   
   public function set_user($s_user){
       $this->_s_user = $s_user;
       return $this;
   }
   
   public function set_password($s_pass){
       $this->_s_password = $s_pass;
       return $this;
   }
   
   public function set_port($i_port){
       $this->_i_port = $i_port;
       return $this;
   }
   
   public function set_user_agent($s_agent){
       $this->_s_user_agent = $s_agent;
       return $this;
   }
   
   public function set_post_data($s_data){
       $this->_s_post_data = $s_data;
       return $this;
   }
   
   public function get_info(){
       return $this->_a_request_info;
   }
   
   public function get_data(){
       return $this->_exec();
   }
   
   /*
    * Кофигурирование CURl
    */
   private function _configure(){
       $this->_o_curl = curl_init();
       if($this->_s_url){
            curl_setopt($this->_o_curl, CURLOPT_URL,$this->_s_url);
            curl_setopt($this->_o_curl, CURLOPT_PORT, $this->_i_port); 
            curl_setopt($this->_o_curl, CURLOPT_RETURNTRANSFER,1); //для возврата результата в виде строки, вместо прямого вывода в браузер
            curl_setopt($this->_o_curl, CURLOPT_CONNECTTIMEOUT, $this->_i_connect_time_out);
            curl_setopt($this->_o_curl, CURLOPT_TIMEOUT, $this->_i_timeout);
            curl_setopt($this->_o_curl, CURLOPT_USERAGENT, $this->_s_user_agent); 
            curl_setopt($this->_o_curl, CURLOPT_FOLLOWLOCATION, 1);
            if($this->_s_user && $this->_s_password){
                curl_setopt($this->_o_curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; 
                curl_setopt($this->_o_curl, CURLOPT_USERPWD, $this->_s_user.":".$this->_s_password);
            }
            if($this->_s_post_data){
                curl_setopt($this->_o_curl, CURLOPT_POST, 1);
                curl_setopt($this->_o_curl, CURLOPT_POSTFIELDS, $this->_s_post_data);
            }
            if($this->_b_show_header){
                curl_setopt($this->_o_curl, CURLOPT_HEADER, true); 
            }else{
                curl_setopt($this->_o_curl, CURLOPT_HEADER, 0); 
            }
            if(count($this->_a_headers)){
                curl_setopt($this->_o_curl, CURLOPT_HTTPHEADER, $this->_a_headers); 
            }
            return true;
        }else{
            Error::trigger('URL is not set to connect');
            return false;
        }
   }
   
   private function _exec(){
       if($this->_configure()){
           $s_data = curl_exec($this->_o_curl);
           if($s_data  === false){
               Error::trigger("CURl Error: ".curl_error($this->_o_curl));
               return false;
           }else{
               $this->_a_request_info = curl_getinfo($this->_o_curl);
               return $s_data;
           }
       }
       $this->_close();
   }
   
   private function _close(){
       curl_close ($this->_o_curl);
   }
}
?>